package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.CommonStatusEnum;
import com.ruoyi.common.exception.tenant.TenantException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTenantPackageMapper;
import com.ruoyi.system.domain.SysTenantPackage;
import com.ruoyi.system.service.ISysTenantPackageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租户套餐Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-08
 */
@Service
public class SysTenantPackageServiceImpl implements ISysTenantPackageService 
{
    @Autowired
    private SysTenantPackageMapper sysTenantPackageMapper;

    @Autowired
    @Lazy
    private ISysTenantService sysTenantService;
    /**
     * 查询租户套餐
     * 
     * @param id 租户套餐主键
     * @return 租户套餐
     */
    @Override
    public SysTenantPackage selectSysTenantPackageById(Long id)
    {
        return sysTenantPackageMapper.selectSysTenantPackageById(id);
    }

    /**
     * 查询租户套餐列表
     * 
     * @param sysTenantPackage 租户套餐
     * @return 租户套餐
     */
    @Override
    public List<SysTenantPackage> selectSysTenantPackageList(SysTenantPackage sysTenantPackage)
    {
        return sysTenantPackageMapper.selectSysTenantPackageList(sysTenantPackage);
    }

    /**
     * 新增租户套餐
     * 
     * @param sysTenantPackage 租户套餐
     * @return 结果
     */
    @Override
    public int insertSysTenantPackage(SysTenantPackage sysTenantPackage)
    {
        return sysTenantPackageMapper.insertSysTenantPackage(sysTenantPackage);
    }

    /**
     * 修改租户套餐
     * 
     * @param sysTenantPackage 租户套餐
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysTenantPackage(SysTenantPackage sysTenantPackage)
    {
        int rs = sysTenantPackageMapper.updateSysTenantPackage(sysTenantPackage);
        List<SysTenant> tenantList = sysTenantService.getTenantListByPackageId(sysTenantPackage.getId());
        tenantList.forEach(tenant->{
            sysTenantService.updateTenantRoleMenu(tenant.getId(),sysTenantPackage.getMenuIds());
        });
        return rs;
    }

    /**
     * 批量删除租户套餐
     * 
     * @param ids 需要删除的租户套餐主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantPackageByIds(Long[] ids)
    {
        return sysTenantPackageMapper.deleteSysTenantPackageByIds(ids);
    }

    /**
     * 删除租户套餐信息
     * 
     * @param id 租户套餐主键
     * @return 结果
     */
    @Override
    public int deleteSysTenantPackageById(Long id)
    {
        return sysTenantPackageMapper.deleteSysTenantPackageById(id);
    }

    @Override
    public SysTenantPackage validTenantPackage(Long packageId) {
        SysTenantPackage tenantPackage = sysTenantPackageMapper.selectSysTenantPackageById(packageId);
        if (tenantPackage == null){
            throw TenantException.TenantPackageNotExistsException();
        }
        if (!tenantPackage.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())){
            throw TenantException.TenantPackageDisableException(tenantPackage.getName());
        }
        return tenantPackage;
    }
}
